   package com.javalong.affairs.module.schedule.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.javalong.affairs.R;
import com.javalong.affairs.base.constant.StatusCode;
import com.javalong.affairs.base.model.SimpleMessage;
import com.javalong.affairs.base.utils.TimeUtils;

public class ScheduleAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private List<SimpleMessage> msgList;
	private Context mContext;

	public ScheduleAdapter(Context context, List<SimpleMessage> msgList) {
		mContext = context;
		inflater = LayoutInflater.from(context);
		this.msgList = msgList;
	}

	@Override
	public int getCount() {
		return msgList.size() + 1;
	}

	@Override
	public Object getItem(int position) {
		return msgList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position + 1;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		if (convertView != null) {
			holder = (ViewHolder) convertView.getTag();
		} else {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_schedule, null);
			holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			holder.tv_msg = (TextView) convertView.findViewById(R.id.tv_msg);
			holder.tv_sender = (TextView) convertView
					.findViewById(R.id.tv_sender);
			holder.tv_dept = (TextView) convertView.findViewById(R.id.tv_dept);
			holder.tv_status = (TextView) convertView
					.findViewById(R.id.tv_status);
			convertView.setTag(holder);
		}
		if (position == 0) {
			holder.tv_time.setText("时间");
			holder.tv_msg.setText("标题");
			holder.tv_dept.setText("部门");
			holder.tv_sender.setText("执行人");
			holder.tv_status.setText("状态");
		} else {
			final SimpleMessage msgInfo = msgList.get(position - 1);
			if (msgInfo.timeBegin != null) {
				holder.tv_time.setText(TimeUtils.getFormatTime(Long
						.parseLong(msgInfo.timeBegin)));
			}
			if (msgInfo.title != null) {
				holder.tv_msg.setText(msgInfo.title);
			}
			if (msgInfo.sectionName != null) {
				holder.tv_dept.setText(msgInfo.sectionName);
			}
			if (msgInfo.name != null) {
				holder.tv_sender.setText(msgInfo.name);
			}
			if (msgInfo.runStatus != null) {
				holder.tv_status.setText(StatusCode.getScheduleStatus(Integer
						.parseInt(msgInfo.runStatus)));
			}
		}
		return convertView;
	}

	class ViewHolder {
		public TextView tv_time;
		public TextView tv_title;
		public TextView tv_msg;
		public TextView tv_dept;
		public Button bt_finish;
		public Button bt_delete;
		public Button bt_search;
		public TextView tv_status;
		public TextView tv_sender;
	}

}
